/**
 * Copyright (c) 2010 by Kevin Bierhoff.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the names of the authors nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.googlecode.syper.test.javasyp.logic;

import com.googlecode.syper.anno.BoundsChecks;
import com.googlecode.syper.anno.Excl;
import com.googlecode.syper.anno.NullChecks;
import com.googlecode.syper.anno.Pred;
import com.googlecode.syper.anno.Result;

import edu.cmu.cs.crystal.annotations.PassingTest;
import edu.cmu.cs.crystal.annotations.UseAnalyses;

/**
 * @author kevin
 * @since Apr 25, 2010
 */
@NullChecks
@BoundsChecks
@PassingTest
@UseAnalyses("SymbolicChecker")
public class ArrayLogic {

	public void simpleTest(@Excl(mod = "[*]") final int[] a) {
		Pred.requires(a != null && 0 < a.length);
		a[0] = 5;
		assert a[0] == 5;
	}
	
	public void twiceTest(@Excl(mod = "[*]") final int[] a) {
		Pred.requires(a != null && 1 < a.length);
		a[0] = 5;
		assert a[0] == 5;
		a[0] = 4;
		a[1] = 5;
		assert a[0] == 4 && a[1] == 5;
	}
	
	public int[] newTest() {
		@Result final int[] a = new int[3];
		assert a != null;
		assert a.length == 3;
		// assert a[1] == 0;
		Pred.ensures(a != null & 0 < a.length);
		return a;
	}
	
	public int[] initTest() {
		int[] a = new int[] { 1, 2, 3 };
		assert a != null;
		assert a.length == 3;
		assert a[1] == 2;
		return a;
	}
	
	public int expressionTest() {
		return new int[] { 1, 3, 5 }[1];
	}
	
	public int expressionTest2() {
		return newTest()[0];
	}
	
	// TODO add support for mod = "[*]"
//	public void comsumeTest() {
//		@Excl(borrowed = false) int[] a = new int[1];
//		a[0] = 5;
//		int[] b = a;
//		assert b.length == 1;
//		assert b[0] == 5;
//		consumeArray(a);
//		assert a.length == 1;
//		assert a[0] == 5;  // THIS SHOULD FAIL
//	}
//	
//	private void consumeArray(@Excl(mod = "[*]") int[] a) {
//		Pred.requires(a != null && 0 < a.length);
//		a[0] = 6; 
//	}
}
